﻿using KMeansAlgorithm;
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LearningWorkbench
{
    public partial class TestController : Form
    {
        public TestController()
        {
            InitializeComponent();
        }

        private void TestController_Load(object sender, EventArgs e)
        {
            var list = DbManager.ListCollections();

            comboBox1.Items.Clear();
            comboBox1.Items.AddRange(list.ToArray());
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var collectionName = comboBox1.SelectedItem as string;
            var scollection = DbManager.GetCollection<LazyBsonDocument>(collectionName);
            var size = scollection.Count();
            var validInputs = 0;
            var correct = 0;

            var dummyFind = scollection.FindOne();
            var dummyTargets = InputTargetHandler.HandleTargets(dummyFind);
            var targetCount = dummyTargets.Count();
            var frequencies = new int[targetCount];
            var results = new List<Tuple<int, double>>();

            foreach (var document in scollection.FindAll())
            {
                var input = InputTargetHandler.HandleInputs(document);
                var target = InputTargetHandler.HandleTargets(document);
                
                if (input != null && target != null)
                {
                    validInputs++;

                    var cluster = WorkbenchModel.ClusterResult.ClusterOf(input);
                    //WorkbenchModel.Anns[cluster].SimulateWith(input);
                    

                    Console.WriteLine("Expected - trained output:");
                    var outputs = WorkbenchModel.BAnns[cluster].ComputeOutputs(input);
                    //WorkbenchModel.Anns[cluster].Outputs;
                    var tindex = Array.IndexOf(target, target.Max());
                    var oindex = Array.IndexOf(outputs, outputs.Max());

                    if (tindex == oindex)
                        correct++;

                    frequencies[tindex]++;

                    //results.Add(new Tuple<int, double>(tindex, outputs[tindex]));
                }
            }

            var successRate = correct / (double)validInputs;
            MessageBox.Show("Total success: " + successRate * 100 + "%\n");
            /*+
                "Number of Valid Inputs: " + validInputs + "\n" +
                "Frequencies: " + string.Join(",",
                          frequencies.Select(x => (x / (double)validInputs).ToString()).ToArray()));
             */ 
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }
    }
}
